/*
 * ${beanName}Controller.java created on ${config.createTime?string("yyyy-MM-dd")} by ${config.createUser}
 */
package ${config.pack}.web;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.BooleanUtils;
import ${config.pack}.entity.${beanName};
import ${config.pack}.entity.${beanShowVo};
import ${config.pack}.entity.${beanQueryVo};
import ${config.pack}.service.${beanName}Service;
import org.eking.framework.common.BusinessMap;
import org.eking.framework.common.Constants;
import org.eking.framework.common.Pager;
import org.eking.framework.common.checkLeak.CSRFTokenManager;
import org.eking.framework.web.BaseExController;
import org.eking.framework.web.system.interceptor.DoLog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * ${config.beanDescription}控制器
 * TODO javadoc for org.eking.${beanName}.web.${beanName}Controller
 * @Copyright: ${config.copyright}
 * @author:${config.createUser}
 * @since: ${config.createTime?string("yyyy-MM-dd HH:mm:ss")}
 */
@Controller
@RequestMapping("/${simpleBeanName}")
public class ${beanName}Controller extends BaseExController {
	
	@Resource(name = "${simpleBeanName}Service")
	private ${beanName}Service ${simpleBeanName}Service;
	
	/**
	 * 跳转${config.beanDescription}首页"
	 * @creator: ${config.createUser}
	 * @createDate: ${config.createTime?string("yyyy-MM-dd")} 
	 * @modifier:
	 * @modifiedDate:
	 * @param request
	 * @param response
	 * @param ${simpleBeanName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toIndex") @DoLog(cnContent="跳转${config.beanDescription}首页") //
	public ModelAndView toIndex(HttpServletRequest request,HttpServletResponse response,${beanName} ${simpleBeanName}) throws Exception{
		HashMap<String, Object> modelMap=new LinkedHashMap<String, Object>();
		modelMap.put("${simpleBeanName}", ${simpleBeanName});
		modelMap.put("csrfToken",  CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("sysMgr/${simpleBeanName}/index").addAllObjects(modelMap);
	}	
	
	/**
	 * 去到${config.beanDescription}列表页面
	 * @creator: ${config.createUser}
	 * @createDate: ${config.createTime?string("yyyy-MM-dd")} 
	 * @modifier:
	 * @modifiedDate:
	 * @param request
	 * @param response
	 * @param ${simpleBeanName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list") @DoLog(cnContent="查看${config.beanDescription}列表信息") //@Permission(name="${simpleBeanName}.view")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,${beanName} ${simpleBeanName}) throws Exception{
		HashMap<String, Object> modelMap=new LinkedHashMap<String, Object>();
		modelMap.put("${simpleBeanName}", ${simpleBeanName});
		modelMap.put("csrfToken",  CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("sysMgr/${simpleBeanName}/list").addAllObjects(modelMap);
	}
	
	/**
	 * ${config.beanDescription}列表ajax分页
	 * @creator: ${config.createUser}
	 * @createDate: ${config.createTime?string("yyyy-MM-dd")}  
	 * @modifier:
	 * @modifiedDate:
	 * @param request
	 * @param response
	 * @param ${simpleBeanName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/ajaxPage") @DoLog(cnContent="${config.beanDescription}列表ajax分页") //@Permission(name="${simpleBeanName}.view")
	public String ajaxPage(HttpServletRequest request,HttpServletResponse response,${beanQueryVo} ${simpleBeanName}) throws Exception{
		try {
				Pager<${beanShowVo}> p = this.parsePager(request);
				Pager<${beanShowVo}> result = ${simpleBeanName}Service.search(p, ${simpleBeanName});
				return this.convertGetPage(response, result);
		} catch (Exception e) {
			return this.handleEx(response, e);
		}
	}
	
	
	/**
	 * 去到添加${config.beanDescription}页面
	 * @creator: ${config.createUser}
	 * @createDate: ${config.createTime?string("yyyy-MM-dd")} 
	 * @modifier:
	 * @modifiedDate:
	 * @param request
	 * @param response
	 * @param ${simpleBeanName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toAdd") @DoLog(cnContent="去到添加${config.beanDescription}页面") //@Permission(name="${simpleBeanName}.add")
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, ${beanName} ${simpleBeanName}) throws Exception {
		HashMap<String, Object> modelMap=new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken",  CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("sysMgr/${simpleBeanName}/add").addAllObjects(modelMap);
	}

	/**
	 * 添加${config.beanDescription}
	 * @creator: ${config.createUser}
	 * @createDate: ${config.createTime?string("yyyy-MM-dd")} 
	 * @modifier:
	 * @modifiedDate:
	 * @param request
	 * @param response
	 * @param ${simpleBeanName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add") @DoLog(cnContent="添加${config.beanDescription}") //@Permission(name="${simpleBeanName}.add")
	public String add(HttpServletRequest request, HttpServletResponse response, ${beanName} ${simpleBeanName}) throws Exception {
		try {
			BusinessMap bm = ${simpleBeanName}Service.create(${simpleBeanName});
			if (bm.getIsSucc()) {
				return this.convertGetObj(response, Constants.SUCCESS);
			} else {
				return this.convertOptFail(response, bm.getAlertMesg());
			}
		} catch (Exception e) {
			return this.handleEx(response, e);
		}
	}
	
	/**
	 * 去到编辑${config.beanDescription}页面
	 * @creator: ${config.createUser}
	 * @createDate: ${config.createTime?string("yyyy-MM-dd")} 
	 * @modifier:
	 * @modifiedDate:
	 * @param request
	 * @param response
	 * @param ${simpleBeanName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toEdit") @DoLog(cnContent="去到编辑${config.beanDescription}页面") //@Permission(name="${simpleBeanName}.edit")
	public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response,String id) throws Exception {
		${beanName} result = ${simpleBeanName}Service.getById(id);
		HashMap<String, Object> modelMap=new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken",  CSRFTokenManager.getTokenForSession(request.getSession()));
		modelMap.put("${simpleBeanName}",result);
		return new ModelAndView("sysMgr/${simpleBeanName}/edit").addAllObjects(modelMap);
	}
	
	/**
	 * 编辑${config.beanDescription}
	 * @creator: ${config.createUser}
	 * @createDate: ${config.createTime?string("yyyy-MM-dd")} 
	 * @modifier:
	 * @modifiedDate:
	 * @param request
	 * @param response
	 * @param ${simpleBeanName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/edit") @DoLog(cnContent="编辑${config.beanDescription}") //@Permission(name="${simpleBeanName}.edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, ${beanName} ${simpleBeanName}) throws Exception {
		try {
			BusinessMap bm = ${simpleBeanName}Service.edit(${simpleBeanName});
			if (bm.getIsSucc()) {
				return this.convertGetObj(response, Constants.SUCCESS);
			} else {
				return this.convertOptFail(response, bm.getAlertMesg());
			}
		} catch (Exception e) {
			return this.handleEx(response, e);
		}
	}
	
	/**
	 * 查看${config.beanDescription}信息
	 * @creator: ${config.createUser}
	 * @createDate: ${config.createTime?string("yyyy-MM-dd")} 
	 * @modifier:
	 * @modifiedDate:
	 * @param request
	 * @param response
	 * @param ${simpleBeanName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/view") @DoLog(cnContent="查看${config.beanDescription}信息") //@Permission(name="${simpleBeanName}.view")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response,String id) throws Exception {
		${beanName} result = ${simpleBeanName}Service.getById(id);
		return new ModelAndView("sysMgr/${simpleBeanName}/view").addObject("${simpleBeanName}",result);
	}
	
	/**
	 * 删除${config.beanDescription}信息
	 * @creator: ${config.createUser}
	 * @createDate: ${config.createTime?string("yyyy-MM-dd")} 
	 * @modifier:
	 * @modifiedDate:
	 * @param request
	 * @param response
	 * @param ${simpleBeanName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete") @DoLog(cnContent="删除${config.beanDescription}信息") //@Permission(name="${simpleBeanName}.delete")
	public String delete(HttpServletRequest request, HttpServletResponse response,String id) throws Exception {
		try {
			 ${simpleBeanName}Service.delete(id);
			return this.convertOptSuccess(response, "删除成功");
		} catch (Exception e) {
			return this.handleEx(response, e);
		}
	}
	
	/**
	 * 返回${config.beanDescription}信息
	 * @creator: ${config.createUser}
	 * @createDate: ${config.createTime?string("yyyy-MM-dd")} 
	 * @modifier:
	 * @modifiedDate:
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 * @throws Exception
	 */	
	@RequestMapping("/getById")
	@DoLog(cnContent = "返回${config.beanDescription}信息")
	public String getById(HttpServletRequest request, HttpServletResponse response, String id) throws Exception {
		try {
			${beanName} result = ${simpleBeanName}Service.getById(id);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("${simpleBeanName}", result);
			return this.convertGetObj(response, map);
		} catch (Exception e) {
			return this.handleEx(response, e);
		}
	}	
}
